import { Body, Controller, Get,Post,Query } from '@nestjs/common';
import { ProductDetailService } from 'src/services/product-detail/product-detail.service'
import { IProductDetailList } from 'src/types/productdetail.interface';
import HttpResponse from 'src/types/http.response.interface';

type UnionType = IProductDetailList[] | IProductDetailList;



@Controller('product')
export class ProductDetailController {
      //等价于上面的写法
      constructor(private readonly  newProductDetailService:ProductDetailService){

      }
      @Get('/detail')  
      async detail(@Query("productId") productId:string):Promise<HttpResponse<IProductDetailList[]>>{
        console.log("查询参数====",productId)
         const data = await this.newProductDetailService.getAllData(productId);
         console.log('查询到的产品明细数据==',data);
         if(Array.isArray(data)){
          return {
              success:true,
              message:"查询数据成功",
              data,
          }
         }
         else{
          return {
              success:false,
              message:"查询数据失败",
              data:[],
          }
         }
         
      }
      /**
     * 用来通过postman或者insomnia来导入数据
     * 在浏览器中输入http://localhost:9898/product/importData
     */
    
    @Post('/importData')
    //DTO Data Transfer Object 数据传输对象，当客户端把数据传送到后台的
    async importData(@Body() requestParamsBody:UnionType) {
      
        const data:UnionType = await this.newProductDetailService.importData(requestParamsBody);
        return {
            success:true,
            message:"数据导入成功",
            data
        };
    }
}




